import * as VueRouter from "vue-router"
import { getCookieSync } from "../utils/cookie";

const routes: VueRouter.RouteRecordRaw[] = [
    {
        path: "/", redirect: "/main"
    },
    {
        path: "/login", component: () => import("../pages/Login/index.vue")
    },
    {
        path: "/main", component: () => import("../pages/AppMain/index.vue"),
        children: [
            {
                path: '/main', component: () => import("../pages/Main/index.vue")
            },
            {
                path: 'new', component: () => import("../pages/New/index.vue")
            },
            {
                path: 'time', component: () => import("../pages/TimeLine/index.vue")
            },
            {
                path: 'photo', component: () => import("../pages/Photo/index.vue")
            }
        ]
    }

]
const router: VueRouter.Router = VueRouter.createRouter({
    routes,
    history: VueRouter.createWebHistory(),
})


router.beforeEach((to, from, next) => {
    const isLogin = getCookieSync('token');
    if (to.path === "/login") {
        next();
    } else if (isLogin) {
        next()
    } else (
        next("/login")
    )
})
export default router;